/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * Nihongo
 * Copyright (C) Massimiliano Maniscalco 2011, 2012 <massi.neptune@yahoo.com>
 * 
Nihongo is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Nihongo is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "test_scelta_mult_kanji.h"

void TestSceltaMultKanji::
run_test ( char *options )
{
	int nr = _contenuto->size();
	_domandeTotLabel->set_text ( Glib::ustring::format ( nr ) );
	_testWin->present ();
	_timer = 0;
	_nrDom = 0;
	_modo = options[0];
	_verso = options[1];
	update_window ();
}

void TestSceltaMultKanji::
update_window ( void )
{
	Glib::ustring _domanda;
	Glib::ustring _primaRisposta;
	Glib::ustring _secondaRisposta;
	Glib::ustring _terzaRisposta;
	Glib::ustring _quartaRisposta;
	_domandaNumLabel->set_text ( Glib::ustring::format ( _nrDom + 1 ) );
	_timerConnection = Glib::signal_timeout ().connect_seconds (sigc::mem_fun (*this, &TestSceltaMultKanji::on_timer_fired), 1 );
	_tempoLabel->set_text ( "00 : 00" );
	switch ( _modo )
	{
		case MOD_KANJI_MEMORY:
			if ( _verso = KNJ_SIGN )
				_domanda = _kanjiCorrente->get_kanji ();
			else
				_domanda = _kanjiCorrente->get_significato ();
			break;
		case MOD_KANJI_READING:
			_domanda = _kanjiCorrente->get_kanji ();
			break;
	}
	_domandaLabel->set_text ( _domanda );
	possibili_risposte ( _primaRisposta, _secondaRisposta, _terzaRisposta, _quartaRisposta );
	_primaRispButton->set_label ( _primaRisposta );
	_secondaRispButton->set_label ( _secondaRisposta );
	_terzaRispButton->set_label ( _terzaRisposta );
	_quartaRispButton->set_label ( _quartaRisposta );
}

void TestSceltaMultKanji::
possibili_risposte ( Glib::ustring &risp1, Glib::ustring &risp2, Glib::ustring &risp3, Glib::ustring &risp4 )
{
	_rispostaBuona = _random->get_int_range (0, 4);
	Glib::ustring _risposte [4];
	const Kanji *_knj;
	for ( char n = 0; n < 4; n++ )
	{
		if ( n == _rispostaBuona )
			_knj = &(*_kanjiCorrente);
		else
			_knj = prendi_kanji_casuale ();
		_risposte [n] = crea_etichetta ( *_knj );
	}
	risp1.assign ( _risposte [0] );
	risp2.assign ( _risposte [1] );
	risp3.assign ( _risposte [2] );
	risp4.assign ( _risposte [3] );
	_rispostaEsatta.assign ( _risposte [ _rispostaBuona ] );
}

const Kanji* TestSceltaMultKanji::
prendi_kanji_casuale ( void )
{
	unsigned int index;
	list<Kanji>::iterator _itKnj = _tutti->begin ();
	index = _random->get_int_range ( 0, _tutti->size() );
	for ( unsigned int n = 0; n < index; n++ )
		_itKnj++;
	return &(*_itKnj);
}

Glib::ustring TestSceltaMultKanji::
crea_etichetta ( const Kanji &knj )
{
	Glib::ustring etichetta;
	switch ( _modo )
	{
		case MOD_KANJI_MEMORY:
			if ( _verso = KNJ_SIGN )
				etichetta = knj.get_significato ();
			else
				etichetta = knj.get_kanji ();
			break;
		case MOD_KANJI_READING:
			etichetta = knj.get_onyomi () + "\n" + knj.get_kunyomi ();
			break;
	}
	return etichetta;
}

void TestSceltaMultKanji::
on_risposta ( char tasto )
{
	bool risultato = ( tasto == _rispostaBuona );
	_timerConnection.disconnect ();
	_kanjiCorrente->test ( _modo, risultato, _timer );
	commit_kanji_changes.emit ( *_kanjiCorrente );
	if ( !risultato )
	{
		Glib::ustring messaggio = "Sbagliato, la risposta giusta è\n<b><big>" + _rispostaEsatta + "</big></b>";
		Gtk::MessageDialog *_alertDialog = new Gtk::MessageDialog ( *_testWin, "", false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
		_alertDialog->set_message ( messaggio, true);
		int res_id = _alertDialog->run();
		delete _alertDialog;
	}
	next_question ();
}

void TestSceltaMultKanji::
next_question ( void )
{
	_timer = 0;
	_nrDom++;
	_kanjiCorrente++;
	if ( _kanjiCorrente == _contenuto->end() )
		_testWin->hide();
	else
		update_window ();
}

bool TestSceltaMultKanji::
show_result ( void )
{
	bool presented;
	_resultCtrl->set_modo ( _modo );
	presented = _resultCtrl->present_window ();
	return presented;
}
